package ru.papergames.model.vo
{
	import flash.events.EventDispatcher;
	
	import mx.utils.ObjectUtil;
	
	public class BaseVO extends EventDispatcher
	{
		
		public var selected:Boolean = false;
		public var pendingUpdate:Boolean = false;
		
		public function BaseVO()
		{
		}
		
		
		
		public function removeUnchangedProperties(toCompare:Object):void {
			var fields:Array = getPostFields();
			
			for each (var field:String in fields) {
				if ( this.hasOwnProperty(field) && toCompare.hasOwnProperty(field) && (this[field] == toCompare[field])) {
					delete toCompare[field];
				}
			}
		}
		
		public function fillFromObject(data:Object):void {			
			var classinfo:Object = ObjectUtil.getClassInfo(data);
            
            for each (var field:QName in classinfo.properties) {
            
            	try {
            		
					if (field && this.hasOwnProperty(field.localName) && data.hasOwnProperty(field.localName)) {
						this[field.localName] = data[field.localName];	
					}
					
				} catch (e:Error) {
        			continue;
        		}
        		
			}		
           
		}
		
		public function compare(o:BaseVO):Boolean {
			return this == o;
		}
		
		public function clone():BaseVO {			
			return ObjectUtil.copy(this) as BaseVO;
		}
		
		protected function getPostFields():Array {
			return [];
		}
		
		public function get postObject():Object {
			var postObj:Object = {};
			
			var fields:Array = getPostFields();
			
			for each (var field:String in fields) {
				if ( this.hasOwnProperty(field) ) postObj[field] = this[field];
			}
			
			return postObj;
		}
		
		public function dispose():void {
			
		}

	}
}